//
//  ST_PairsDismissAnimator.swift
//  btvideo
//
//  Created by mac on 2020/1/6.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_PairsDismissAnimator: NSObject ,UIViewControllerAnimatedTransitioning{
 
    var operationView:ST_PairsShareParticularView
    
    init(sharedPlayView:ST_PairsShareParticularView) {
        operationView = sharedPlayView
        super.init()
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.6
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
//        UIViewPropertyAnimator
        
        guard let nav = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? UINavigationController ,let centerVC = nav.topViewController else{ return }

        
        let container = transitionContext.containerView
        
        let playerView = operationView
        playerView.phDelegate = nil
         
        let currentRect = centerVC.view.convert(playerView.frame, to: nil)
        
        print("交互动画：当前位置:",currentRect,"----还好:",playerView.frame)
        
        playerView.removeFromSuperview()
        
        centerVC.view.isHidden = true
        
        container.addSubview(playerView)
        
        playerView.frame = currentRect
        
        playerView.cornerRadius = 20
        
        print("交互动画：动画位置:",playerView.present_start_rect)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: [], animations: {
            
            playerView.frame = playerView.present_start_rect
            
//            playerView.rightSignLabel.alpha = 1
            
        }) { (finished) in
            
            
            if let cell = playerView.fromeController?.present_start_cell(){
                                    
                    playerView.removeFromSuperview()
                    
                    cell.contentView.addSubview(playerView)
                    
                    playerView.refresh_data()
                    
                    playerView.frame = CGRect.init(origin: CGPoint.zero, size: playerView.present_start_rect.size)
                    
                    playerView.isUserInteractionEnabled = false

                    print("交互动画：结束位置:",playerView.frame)
                
                }
            
            
                transitionContext.completeTransition(true)
        }
        
    }
    

    
    
    
}
